package com.arpa.oms.controller;

import com.arpa.core.web.MediaTypes;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.oms.domain.dto.OmsAllocationItemDTO;
import com.arpa.oms.domain.entity.OmsAllocationItem;
import com.arpa.oms.domain.vo.OmsAllocationItemVO;
import com.arpa.oms.service.IOmsAllocationItemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * <p>
 * OMS调拨单明细 Controller
 * </p>
 *
 * @author LEO
 * @since 2020-11-17
 */
@Api(tags = "OMS调拨单明细")
@RestController
@RequestMapping(value="/omsAllocationItem", produces = "application/json;charset=UTF-8")
@Log4j2(topic = "business")
public class OmsAllocationItemController {

   @Autowired
   private IOmsAllocationItemService  iOmsAllocationItemService;

    /**
     * 获取 OMS调拨单明细 列表
     *
     * @param omsAllocationItemDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 OMS调拨单明细 列表", notes = "获取 OMS调拨单明细 列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = OmsAllocationItemVO.class))
    @NeedLogin
    @GetMapping()
    public Result list(OmsAllocationItemDTO omsAllocationItemDTO,HttpServletRequest request) {
        try {
            OmsAllocationItemVO omsAllocationItemVOSum = iOmsAllocationItemService.queryListSum(omsAllocationItemDTO);
            List<OmsAllocationItemVO> omsAllocationItemVOList = iOmsAllocationItemService.queryList(omsAllocationItemDTO);

            Page returnPage = new Page(omsAllocationItemDTO.getPageNum(),omsAllocationItemDTO.getPageSize());
            returnPage.setRecords(omsAllocationItemVOList);
            returnPage.setTotal(omsAllocationItemVOSum.getTotal());
            returnPage.setSumObject(omsAllocationItemVOSum);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 添加 OMS调拨单明细
     *
     * @param entity OmsAllocationItem
     * @param request
     * @return
     */
    @ApiOperation(value = "添加 OMS调拨单明细 ", notes = "添加 OMS调拨单明细 ")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
    })
    @NeedLogin
    @PostMapping()
    public Result create(OmsAllocationItem entity, HttpServletRequest request) {
        try {
            iOmsAllocationItemService.save(entity);
            return Result.ok("添加成功");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
    * 获取OMS调拨单明细详情
    *
    * @param code
    * @param request
    * @return
    */
    @ApiOperation(value = "获取OMS调拨单明细详情", notes = "获取OMS调拨单明细详情")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
        @ApiImplicitParam(name = "code", value = "OMS调拨单明细code", paramType = "path", dataType = "String")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = OmsAllocationItem.class))
    @NeedLogin
    @GetMapping(value = "/{code}")
    public Result omsAllocationItem(@PathVariable String code, HttpServletRequest request) {
        try {
            OmsAllocationItem entity = iOmsAllocationItemService.getOne(new QueryWrapper<OmsAllocationItem>().lambda().eq(OmsAllocationItem::getCode,code));
            return Result.ok().data(entity);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改OMS调拨单明细
     *
     * @param entity OMS调拨单明细
     * @param request
     * @return
     */
    @ApiOperation(value = "修改OMS调拨单明细", notes = "修改OMS调拨单明细")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
    })
    @NeedLogin
    @PutMapping()
    public Result update(OmsAllocationItem entity,HttpServletRequest request) {
        if(StringUtils.isBlank(entity.getCode())){
            return Result.error("code不能为空");
        }
        int updateNum = iOmsAllocationItemService.updateByCode(entity);
        if(updateNum == 0){
            return Result.error("没有更新数据！code:"+entity.getCode());
        }else{
            return Result.ok("修改成功,更新"+updateNum+"条数据！");
        }
    }

    /**
     * 删除 OMS调拨单明细
     *
     * @param codes
     * @param request
     * @return
     */
    @ApiOperation(value = "删除 OMS调拨单明细 ", notes = "删除 OMS调拨单明细 ", produces = MediaTypes.JSON_UTF_8)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "codes", value = "调拨单明细标识", required = true, paramType = "query", dataType = "List")
    })
    @NeedLogin
    @DeleteMapping(value = "")
    public Result batchdel(@RequestParam List<String> codes, HttpServletRequest request) {
        try {
            iOmsAllocationItemService.remove(new QueryWrapper<OmsAllocationItem>().lambda().in(OmsAllocationItem::getCode,codes));
            return Result.ok();
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

 }
